<?php

namespace App\Http\Controllers\Data;

use App\Http\Controllers\Controller;
use App\Models\Honour;
use App\Models\News;
use App\Models\Product;


class AllDataController extends Controller
{
    //

    public function AllData()
    {

        $re1 = Product::orderBy('time', 'desc')->get();
        $re2 = News::orderBy('time', 'desc')->get();
        $re3 = Honour::orderBy('time', 'desc')->get();
        $honour = null;
        $product = null;
        $news = null;
        for ($i = 0; $i < sizeof($re1); $i++) {
            $product[$i]['img'] = $re1[$i]['img'];
            $product[$i]['title'] = $re1[$i]['title'];
            $product[$i]['brief'] = $re1[$i]['brief'];
            $product[$i]['id'] = $re1[$i]['id'];
            $product[$i]['SCategories'] = $re1[$i]['SCategories'];
            $product[$i]['LCategories'] = $re1[$i]['LCategories'];

        }

        for ($i = 0; $i < sizeof($re2); $i++) {
            $news[$i]['img'] = $re2[$i]['img'];
            $news[$i]['title'] = $re2[$i]['title'];
            $news[$i]['content'] = $re2[$i]['content'];
            $news[$i]['id'] = $re2[$i]['id'];
            $news[$i]['time'] = $re2[$i]['time'];
            $news['categories'] = $re2[$i]['categories'];
        }

        for ($i = 0; $i < sizeof($re3); $i++) {
            $honour[$i]['img'] = $re3[$i]['img'];
            $honour[$i]['name'] = $re3[$i]['name'];
            $honour[$i]['id'] = $re3[$i]['id'];
            $honour['categories'] = $re3[$i]['categories'];
        }

        $myfile = fopen('./contact.txt', 'r') or die('Unable to open file!');
        $file = fread($myfile, filesize('./contact.txt'));
        $array = explode('|分割|', $file);
        $contact = array();
        for ($i = 0; $i < sizeof($array); ++$i) {
            $t = explode(':', $array[$i]);
            $contact[$t[0]] = $t[1];
        }

        $myfile = fopen('./information.txt', 'r') or die('Unable to open file!');
        $file = fread($myfile, filesize('./information.txt'));
        $array = explode('|分隔|', $file);
        $information = array();
        for ($i = 0; $i < sizeof($array); ++$i) {
            $t = explode(':', $array[$i]);
            $information[$t[0]] = $t[1];
        }

        $data = ['news' => $news, 'product' => $product, 'honour' => $honour, 'information' => $information, 'contact' => $contact];
        return json_encode($data);

        // fake data
        // $product['img']='./news/00001.jpg';
        // $product['title']='声波传感器的应用';
        // $product['id']='00001';
        // $product['time']='2019-7-25';
        // $data['SCategories']='压电解决方案';
        // $data['LCategories']='超声波液体/物体传感器';

        // $news['img']='./news/00001.jpg';
        // $news['title']='声波传感器的应用';
        // $news['content']='超声波的工作原理，重要性能以及其他应用';
        // $news['id']='00001';
        // $news['time']='2019-7-25';
        // $news['categories']='公司新闻'

        // $honour['img']='./honour/00001.jpg';
        // $honour['name']='IATF证书';
        // $honour['id']='00001';
        // $honour['categories']='公司荣誉';

        // $data = ['news' => $news, 'product' => $product, 'honour' => $honour];
        // return $data;
    }
}
